package com.virtualtrainer_01.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;

import com.virtualtrainer_01.model.Food;
import com.virtualtrainer_01.model.Model;
import com.virtualtrainer_01.model.Training;
import com.virtualtrainer_01.model.TrainingDay;

public class TrainingDayDB extends SQLiteDB {
	public static final String KEY_ID = "_id";
	public static final String KEY_IDTRAINING = "idTraining";
	public static final String KEY_BREACKFAST = "breakfast"; // idFood
	public static final String KEY_DINNER = "dinner"; // idFood
	public static final String KEY_LUNCH = "lunch"; // idFood

	public TrainingDayDB(Context c) {
		super(c, DATABASE_TABLE);
	}

	public static final String DATABASE_TABLE = "trainingday";

	@Override
	public String getData() {
		String[] columns = new String[] { KEY_ID, KEY_IDTRAINING,
				KEY_BREACKFAST, KEY_LUNCH, KEY_DINNER };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ID);
		int iIdTr = c.getColumnIndex(KEY_IDTRAINING);
		int iBr = c.getColumnIndex(KEY_BREACKFAST);
		int iLu = c.getColumnIndex(KEY_LUNCH);
		int iDi = c.getColumnIndex(KEY_DINNER);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = result + c.getString(iRow) + " " + c.getString(iIdTr)
					+ " " + c.getString(iBr) + " " + c.getString(iLu) + " "
					+ c.getString(iDi);
		return result;
	}

	@Override
	public long createEntry(Model entry) {
		TrainingDay td = (TrainingDay) entry;
		TrainingDB tdb = new TrainingDB(ourContext);
		FoodDB fdb = new FoodDB(ourContext);
		tdb.open();
		fdb.open();
		ContentValues cv = new ContentValues();
		try {
			cv.put(KEY_IDTRAINING, tdb.getId(td.getTraining()));
			cv.put(KEY_BREACKFAST, fdb.getId(td.getBreakfast()));
			cv.put(KEY_LUNCH, fdb.getId(td.getLunch()));
			cv.put(KEY_DINNER, fdb.getId(td.getDinner()));
			ourDatabase.insert(DATABASE_TABLE, null, cv);
		} catch (SQLiteException e) {
			e.printStackTrace();

		}
		fdb.close();
		tdb.close();
		return 1;
	}

	public List<TrainingDay> getTrainingDayList() {
		List<TrainingDay> list = new ArrayList<TrainingDay>();
		String[] columns = new String[] { KEY_IDTRAINING,
				KEY_BREACKFAST, KEY_LUNCH, KEY_DINNER };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);

		int iIdTr = c.getColumnIndex(KEY_IDTRAINING);
		int iBr = c.getColumnIndex(KEY_BREACKFAST);
		int iLu = c.getColumnIndex(KEY_LUNCH);
		int iDi = c.getColumnIndex(KEY_DINNER);
		
		TrainingDB tdb = new TrainingDB(ourContext);
		tdb.open();
		FoodDB fdb = new FoodDB(ourContext);
		fdb.open();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
		{
			Training t = tdb.getTraining(Integer.parseInt(c.getString(iIdTr)));
			Food breackfast = fdb.getFood(Integer.parseInt(c.getString(iBr)));
			Food lunch = fdb.getFood(Integer.parseInt(c.getString(iLu)));
			Food dinner = fdb.getFood(Integer.parseInt(c.getString(iDi)));
			list.add(new TrainingDay(t, breackfast, lunch, dinner));
		}
		fdb.close();
		tdb.close();
		return list;
	}
}
